package com.ymatou.iapi.doorgod.apigateway.testcase;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.doorgod.apigateway.parameter.ApigatewayBean;
import com.ymatou.iapi.doorgod.apigateway.service.ApigatewayGetCall;
import com.ymatou.iapi.doorgod.apigateway.service.ApigatewayPostCall;
import com.ymatou.iapi.doorgod.apigateway.service.Utils;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
/**
*门神验签
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_apigateway {

	private static ApigatewayGetCall call;

	private final static String URL = "http://h5.ymatou.com/warmup";

	private static List<String> commonField = Arrays.asList("app-key",
			"app-version", "deviceid", "accept-version", "timestamp",
			"ymt-req", "sign-version");

	private static Map<String, String> param = new TreeMap<String, String>();

	private static String secret = "zxcvbnm1234567890";

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("门神验签");
	}

	@Before
	public void caseUp() {

		call = new ApigatewayGetCall();

	}

	@AfterClass
	public static void tearDown() {
		//Logger.generateResult(decisionengineCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_apigateway_001() {
		Logger.start(true, "验签通过");
		try {

			param.put("app-key", "test_app");
			param.put("app-version", "1.0");
			param.put("deviceid", "xxx13-xxsdfasjfdjsajfdsxfdfd");
			param.put("accept-version", "1.02");
			param.put("timestamp", System.currentTimeMillis() + "");
			param.put("ymt-req", "");
			param.put("sign-version", "1.0");

			String sign = Utils.generateMD5(param, secret);

			param.put("sign", sign);

			param.forEach((k, v) -> {

				call.addHeader(k, v);

			});
			call.setProxy("127.0.0.1", 8888);
			call.callService();

			Logger.info("code:" + call.getStatusCode());

			Logger.verifyEquals(404, call.getStatusCode(), "验证返回code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_apigateway_002() {
		Logger.start(true, "sign不对，门神验签失败，档掉");
		try {

			param.put("app-key", "test_app");
			param.put("app-version", "1.0");
			param.put("deviceid", "xxx13-xxsdfasjfdjsajfdsxfdfd");
			param.put("accept-version", "1.02");
			param.put("timestamp", System.currentTimeMillis() + "");
			param.put("ymt-req", "");
			param.put("sign-version", "1.0");

			//String sign = Utils.generateMD5(param, secret);

			String sign = "sss";

			param.put("sign", sign);

			param.forEach((k, v) -> {

				call.addHeader(k, v);

			});

			call.callService();

			Logger.info("code:" + call.getStatusCode());

			Logger.verifyEquals(403, call.getStatusCode(), "验证返回code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_apigateway_003() {
		Logger.start(true, "必须的公共头部参数不足7个,默认为空值");
		try {

			param.put("app-key", "test_app");
			param.put("app-version", "1.0");
			param.put("deviceid", "xxx13-xxsdfasjfdjsajfdsxfdfd");
			param.put("accept-version", "1.02");
			param.put("timestamp", System.currentTimeMillis() + "");
			param.put("ymt-req", "");
			param.put("sign-version", "1.0");

			String sign = Utils.generateMD5(param, secret);

			param.put("sign", sign);

			param.forEach((k, v) -> {

				call.addHeader(k, v);

			});

			call.removeHeader("ymt-req");

			call.callService();

			Logger.info("code:" + call.getStatusCode());

			Logger.verifyEquals(404, call.getStatusCode(), "验证返回code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_apigateway_004() {
		Logger.start(true, "公共头部参数超过7个,header还有其它参数");
		try {

			param.put("app-key", "test_app");
			param.put("app-version", "1.0");
			param.put("deviceid", "xxx13-xxsdfasjfdjsajfdsxfdfd");
			param.put("accept-version", "1.02");
			param.put("timestamp", System.currentTimeMillis() + "");
			param.put("ymt-req", "");
			param.put("sign-version", "1.0");

			String sign = Utils.generateMD5(param, secret);

			param.put("ymt-part", "sdfsdfsdfsdfsdfsdfsdf");
			param.put("ymt-part2", "sdfsdfsdfsdfsdfsdfsdf");

			param.put("sign", sign);

			param.forEach((k, v) -> {

				call.addHeader(k, v);

			});

			call.callService();

			Logger.info("code:" + call.getStatusCode());

			Logger.verifyEquals(404, call.getStatusCode(), "验证返回code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_apigateway_005() {
		Logger.start(true, "sign-version 版本不对，验签不过");
		try {

			param.put("app-key", "test_app");
			param.put("app-version", "1.0");
			param.put("deviceid", "xxx13-xxsdfasjfdjsajfdsxfdfd");
			param.put("accept-version", "1.02");
			param.put("timestamp", System.currentTimeMillis() + "");
			param.put("ymt-req", "");
			param.put("sign-version", "2.0");

			String sign = Utils.generateMD5(param, secret);

			param.put("sign", sign);

			param.forEach((k, v) -> {

				call.addHeader(k, v);

			});

			call.callService();

			Logger.info("code:" + call.getStatusCode());

			Logger.verifyEquals(403, call.getStatusCode(), "验证返回code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_apigateway_006() {
		Logger.start(true, "secret 不对，验签不过");
		try {

			param.put("app-key", "test_app");
			param.put("app-version", "1.0");
			param.put("deviceid", "xxx13-xxsdfasjfdjsajfdsxfdfd");
			param.put("accept-version", "1.02");
			param.put("timestamp", System.currentTimeMillis() + "");
			param.put("ymt-req", "");
			param.put("sign-version", "2.0");

			secret = "abcdefg";

			String sign = Utils.generateMD5(param, secret);

			param.put("sign", sign);

			param.forEach((k, v) -> {

				call.addHeader(k, v);

			});

			call.callService();

			Logger.info("code:" + call.getStatusCode());

			Logger.verifyEquals(403, call.getStatusCode(), "验证返回code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_apigateway_007() {
		Logger.start(true, "没有 sign 头部参数，验签不过");
		try {

			param.put("app-key", "test_app");
			param.put("app-version", "1.0");
			param.put("deviceid", "xxx13-xxsdfasjfdjsajfdsxfdfd");
			param.put("accept-version", "1.02");
			param.put("timestamp", System.currentTimeMillis() + "");
			param.put("ymt-req", "");
			param.put("sign-version", "2.0");

			secret = "abcdefg";

			String sign = Utils.generateMD5(param, secret);

			//param.put("sign", sign);

			param.forEach((k, v) -> {

				call.addHeader(k, v);

			});

			call.callService();

			Logger.info("code:" + call.getStatusCode());

			Logger.verifyEquals(403, call.getStatusCode(), "验证返回code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_apigateway_008() {
		Logger.start(true, "没有 sign-version 头部参数，验签不过");
		try {

			param.put("app-key", "test_app");
			param.put("app-version", "1.0");
			param.put("deviceid", "xxx13-xxsdfasjfdjsajfdsxfdfd");
			param.put("accept-version", "1.02");
			param.put("timestamp", System.currentTimeMillis() + "");
			param.put("ymt-req", "");
			//param.put("sign-version", "2.0");

			secret = "abcdefg";

			String sign = Utils.generateMD5(param, secret);

			param.put("sign", sign);

			param.forEach((k, v) -> {

				call.addHeader(k, v);

			});

			call.callService();

			Logger.info("code:" + call.getStatusCode());

			Logger.verifyEquals(403, call.getStatusCode(), "验证返回code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_apigateway_009() {
		Logger.start(true, "get 请求url 带请求参数");
		try {

			param.put("app-key", "test_app");
			param.put("app-version", "1.0");
			param.put("deviceid", "xxx13-xxsdfasjfdjsajfdsxfdfd");
			param.put("accept-version", "1.02");
			param.put("timestamp", System.currentTimeMillis() + "");
			param.put("ymt-req", "");
			param.put("sign-version", "1.0");

			param.put("a", "bbb");
			param.put("b", "aaa");

			String sign = Utils.generateMD5(param, secret);

			param.put("sign", sign);

			call.setUrl(String.format("%s?a=bbb&b=aaa", URL));

			param.forEach((k, v) -> {

				call.addHeader(k, v);

			});

			call.removeHeader("a");
			call.removeHeader("b");

			call.callService();

			Logger.info("code:" + call.getStatusCode());

			Logger.verifyEquals(404, call.getStatusCode(), "验证返回code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_apigateway_010() {
		Logger.start(true, "get 请求url中有跟header重置的key");
		try {

			param.put("app-key", "test_app");
			param.put("app-version", "1.0");
			param.put("deviceid", "xxx13-xxsdfasjfdjsajfdsxfdfd");
			param.put("accept-version", "1.02");
			param.put("timestamp", System.currentTimeMillis() + "");
			param.put("ymt-req", "");
			param.put("sign-version", "1.0");

			param.put("a", "bbb");
			param.put("b", "aaa");

			String sign = Utils.generateMD5(param, secret);

			param.put("sign", sign);

			call.setUrl(String.format("%s?a=bbb&b=ccc", URL));

			param.forEach((k, v) -> {

				call.addHeader(k, v);

			});

			call.removeHeader("a");
			//call.removeHeader("b");

			call.callService();

			Logger.info("code:" + call.getStatusCode());

			Logger.verifyEquals(404, call.getStatusCode(), "验证返回code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_apigateway_011() {
		Logger.start(true, "post 请求带请求body");
		try {

			ApigatewayPostCall postCall = new ApigatewayPostCall();
			ApigatewayBean bean = new ApigatewayBean();

			param.put("app-key", "test_app");
			param.put("app-version", "1.0");
			param.put("deviceid", "xxx13-xxsdfasjfdjsajfdsxfdfd");
			param.put("accept-version", "1.02");
			param.put("timestamp", System.currentTimeMillis() + "");
			param.put("ymt-req", "");
			param.put("sign-version", "1.0");

			String sign = Utils.generateMD5(param, secret);

			param.put("sign", sign);

			param.forEach((k, v) -> {

				postCall.addHeader(k, v);

			});

			postCall.setData(bean);
			postCall.callService();

			Logger.info("code:" + postCall.getStatusCode());

			Logger.verifyEquals(404, postCall.getStatusCode(), "验证返回code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_apigateway_012() {
		Logger.start(true, "万能签名 验签通过");
		try {

			param.put("app-key", "test_app");
			param.put("app-version", "1.0");
			param.put("deviceid", "xxx13-xxsdfasjfdjsajfdsxfdfd");
			param.put("accept-version", "1.02");
			param.put("timestamp", System.currentTimeMillis() + "");
			param.put("ymt-req", "");
			param.put("sign-version", "1.0");

			//String sign = Utils.generateMD5(param, secret);

			param.put("sign", "ymatou");

			param.forEach((k, v) -> {

				call.addHeader(k, v);

			});

			call.callService();

			Logger.info("code:" + call.getStatusCode());

			Logger.verifyEquals(404, call.getStatusCode(), "验证返回code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}